<template>
  <el-dialog title="业绩分配" width="40%" :before-close="dialogClose" :visible.sync="show">
    <div class="moneyText">
        业绩总额:{{pay}}
    </div>
    <el-table v-show="shareArr.length > 0" :data="shareArr" style="width: 100%" row-key="id" 
    border lazy :load="load">
        <el-table-column prop="name" label="员工姓名" align="center"></el-table-column>
        <el-table-column prop="money" label="分配业绩" align="center"></el-table-column>
    </el-table>
    <el-table v-show="shareArr.length == 0" :data="userArr" style="width: 100%" row-key="id" 
    border lazy :load="load">
        <el-table-column prop="realname" label="员工姓名" align="center"></el-table-column>
        <el-table-column label="分配业绩" align="center">
          <template slot-scope="scope">
              <el-input v-model="scope.row.money" size="mini" class="select_input"></el-input>
          </template>
        </el-table-column>
    </el-table>
    <div align="center" style="margin-top:10px;" v-show="shareArr.length == 0">
      <el-button type="primary" @click="addSubmit">确 定</el-button>
      <el-button @click="dialogClose">取 消</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
export default {
  name: 'share',
  props:['show','userArr','id','pay','type','shareArr'],
  data() {
    return {
      shareList:[]
    }
  },
  created: function() {
    
  },
  watch: {
      //正确给 cData 赋值的 方法
      shareArr: function(newVal,oldVal){
          this.shareList = newVal;  //newVal即是chartData
          //this.$forceUpdate()
      }
  },
  methods: {
    dialogClose:function(){
      this.$emit("func",{close:true})
    },
    load(tree, treeNode, resolve) {
      setTimeout(() => {
        resolve()
      }, 1000)
    },
    addSubmit:function(){
      var money = 0
			for(var x in this.userArr){
        if(this.userArr[x]['money']){
          money += this.userArr[x]['money'] - 1 + 1
        }
			}
      if(money != this.pay){
        this.$message.error('分成金额和总数不一致!');
        return;
			}
      var data = {
				oa_id:localStorage.getItem('oa_id'),
				order_id:this.id,
				arr:this.userArr,
				type:this.type
			}
      var that = this
      this.$post('manage/share/add',data).then((res) => {
          if(res.code == 1){
            that.$message.success('业绩分成成功!!');
            that.$emit("func",{close:true})
          }else{
            that.$message.error('业绩分失败!!!');
          }
      })
    }
  }
}
</script>
<style>
.moneyText{
  width:95%;
	border:1px solid #C8C7CC;
	height:35px;
	line-height:35px;
	text-align: center;
  margin-bottom:10px;
}
</style>